/*
 * ModellView.java
 *
 * Created on 2007. május 12., 21:47
 */

package lemmings;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 *
 * @author  weepee
 */
public class ModellView extends javax.swing.JFrame {
    
    static private Modell modell = null;
    private String lastSelected=null;
    javax.swing.Timer timer;
    String stageState=null;
    
    ActionListener ticker=new ActionListener(){
        public void actionPerformed(ActionEvent evt) {
            if (modell!=null){
                stageState=modell.nextStep();
                canvas.repaint();
            }
        }
    };
    
    /** Creates new form ModellView */
    public ModellView() {
        initComponents();
    }
    
    private class LemmingCanvas extends java.awt.Canvas{
        private int width;
        private int height;
        
        private int actX=0;
        private int actY=0;
        
        private void drawField(Color c,Graphics g){
            g.setColor(c);
            g.fillRect(actX,actY,width,height);
            actX+=width;
            
        }
        
        public void paint(java.awt.Graphics g){
            String[] rows=stageState.split("\n");
            width=Math.round(canvas.getWidth()/rows[0].length());
            height=Math.round(canvas.getHeight()/rows.length);
        
            actX=0;
            actY=0;

            for (int i=0;i<rows.length;i++){
                actX=0;
                for (int j=0;j<rows[i].length();j++){
                    if(rows[i].substring(j,j+1).equals("0")){
                        drawField(Color.WHITE,g);
                    }else if (rows[i].substring(j,j+1).equals("K")){
                        drawField(Color.BLACK,g);
                    }else if (rows[i].substring(j,j+1).equals("B")){
                        drawField(Color.GREEN,g);
                    }else if (rows[i].substring(j,j+1).equals("F")){
                        drawField(Color.YELLOW,g);
                    }else{
                        drawField(Color.RED,g);
                    }
                }
                actY+=height;
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        canvas = new LemmingCanvas();
        jPanel1 = new javax.swing.JPanel();
        exitGame = new javax.swing.JButton();
        newGame = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        drill = new javax.swing.JButton();
        build = new javax.swing.JButton();
        parachute = new javax.swing.JButton();
        explode = new javax.swing.JButton();
        stoned = new javax.swing.JButton();
        undo = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        highScores = new javax.swing.JButton();
        nuke = new javax.swing.JButton();
        pause = new javax.swing.JButton();
        fastForward = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        canvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                canvasMouseClicked(evt);
            }
        });

        exitGame.setText("Exit Game");
        exitGame.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exitGameMouseClicked(evt);
            }
        });

        newGame.setText("New Game");
        newGame.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                newGameMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, newGame, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, exitGame, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(newGame)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(exitGame)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        drill.setText("Drill");
        drill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                drillMouseClicked(evt);
            }
        });

        build.setText("Build");
        build.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buildMouseClicked(evt);
            }
        });

        parachute.setText("Parachute");
        parachute.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                parachuteMouseClicked(evt);
            }
        });

        explode.setText("Explode");
        explode.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                explodeMouseClicked(evt);
            }
        });

        stoned.setText("Stoned");
        stoned.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                stonedMouseClicked(evt);
            }
        });

        undo.setText("Undo");
        undo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                undoMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(parachute, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(build, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(stoned, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, undo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, explode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, drill, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(build)
                    .add(drill))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(parachute)
                    .add(explode))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(undo)
                    .add(stoned))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        highScores.setText("High Scores");

        nuke.setText("Nuke");

        pause.setText("Pause");
        pause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pauseMouseClicked(evt);
            }
        });

        fastForward.setText("Fast Forward");
        fastForward.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fastForwardMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(highScores, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .add(nuke, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .add(pause, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .add(fastForward, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(highScores)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nuke)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pause)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(fastForward))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(canvas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 400, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(12, 12, 12)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(canvas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fastForwardMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fastForwardMouseClicked
        if(timer.getDelay()<2000){
            timer.setDelay(2000);
            timer.setInitialDelay(0);
            fastForward.setText("Fast Forward");
        }else{
            timer.setDelay(500);
            fastForward.setText("Normal Tempo");
        }
    }//GEN-LAST:event_fastForwardMouseClicked

    private void pauseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseMouseClicked
        if (timer.isRunning()){
            timer.stop();
            pause.setText("Resume");
        }else{
            timer.start();
            pause.setText("Pause");
        }
    }//GEN-LAST:event_pauseMouseClicked
    
    private void canvasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseClicked
        int cX=evt.getX();
        int cY=evt.getY();
        
        if (lastSelected != null) {
            if (lastSelected.compareTo("block") == 0) {
                if (GetStoned.getNum() > 0) {
                    modell.addTool(cX, cY, new GetStoned());
                    GetStoned.setNum(GetStoned.getNum() - 1);
                }
            } else if (lastSelected.compareTo("explode") == 0) {
                if (Explode.getNum() > 0) {
                    modell.addTool(cX, cY, new Explode());
                    Explode.setNum(Explode.getNum() - 1);
                }
            } else if (lastSelected.compareTo("build") == 0) {
                if (Build.getNum() > 0) {
                    modell.addTool(cX, cY, new Build());
                    Build.setNum(Build.getNum() - 1);
                }
            } else if (lastSelected.compareTo("drill") == 0) {
                if (Drill.getNum() > 0) {
                    modell.addTool(cX, cY, new Drill());
                    Drill.setNum(Drill.getNum() - 1);
                }
            } else if (lastSelected.compareTo("para") == 0) {
                if (Parachute.getNum() > 0) {
                    modell.addTool(cX, cY, new Parachute());
                    Parachute.setNum(Parachute.getNum() - 1);
                }
            }
            lastSelected = null;
        }
    }//GEN-LAST:event_canvasMouseClicked
    
    private void undoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_undoMouseClicked
// TODO add your handling code here:
    }//GEN-LAST:event_undoMouseClicked
    
    private void explodeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_explodeMouseClicked
        lastSelected=new String("explode");
    }//GEN-LAST:event_explodeMouseClicked
    
    private void stonedMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stonedMouseClicked
// TODO add your handling code here:
        lastSelected=new String("block");
    }//GEN-LAST:event_stonedMouseClicked
    
    private void parachuteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parachuteMouseClicked
// TODO add your handling code here:
        lastSelected=new String("para");
    }//GEN-LAST:event_parachuteMouseClicked
    
    private void drillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_drillMouseClicked
        lastSelected=new String("drill");
    }//GEN-LAST:event_drillMouseClicked
    
    private void buildMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buildMouseClicked
        lastSelected=new String("build");
    }//GEN-LAST:event_buildMouseClicked
    
    private void newGameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newGameMouseClicked
        
        String name=(String)JOptionPane.showInputDialog("Please enter your name!");
        modell=new Modell(name);
        timer=new Timer(2000,ticker);
        timer.start();
    }//GEN-LAST:event_newGameMouseClicked
    
    private void exitGameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitGameMouseClicked
        System.exit(-1);
    }//GEN-LAST:event_exitGameMouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ModellView().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton build;
    private java.awt.Canvas canvas;
    private javax.swing.JButton drill;
    private javax.swing.JButton exitGame;
    private javax.swing.JButton explode;
    private javax.swing.JButton fastForward;
    private javax.swing.JButton highScores;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton newGame;
    private javax.swing.JButton nuke;
    private javax.swing.JButton parachute;
    private javax.swing.JButton pause;
    private javax.swing.JButton stoned;
    private javax.swing.JButton undo;
    // End of variables declaration//GEN-END:variables
    
}
